import axios from 'axios'
import { IPage,IArticle, IUser, IArtItem } from '../types'
interface BaseRes {
    stat: string
    data?: string
}
interface LoginRes{
    stat:string,
    data?:{token:string}
}

interface UserRes{
    stat:string,
    data?:IUser
}
interface ArticleRes{
    stat:string,
    data?:{
        items:IArtItem[],
        total:number
    }
}
interface ArtGetRes{
    stat:string,
    data?:{
        info:IArtItem,
        total:number
    }
}


//登录
export async function login(account:string, pwd:string) {
    let result = await axios.post<LoginRes>('/api/auth/login', {account:account, pwd:pwd})
    return result.data
}
//注册
export async function register(account:string, pwd:string) {
    let result = await axios.post<BaseRes>('/api/auth/registry', {account:account, pwd:pwd})
    return result.data
}
//获取用户信息
export async function getUserInfo() {
    let result = await axios.get<UserRes>('/api/user/userInfo')
    return result.data
}
//修改用户信息
export async function updateUserInfo(user:IUser) {
    let result = await axios.put<UserRes>('/api/user/updateUserInfo',user)
    return result.data
}
//退出登录
export async function logout() {
    let result = await axios.post<BaseRes>('/api/auth/logout')
    return result.data
}

//获取所有文章
export async function searchAll(page:IPage) {
    let result = await axios.post<ArticleRes>('/api/article/searchall', page)
    return result.data
}

//获取本人文章
export async function searchOwner(page:IPage) {
    let result = await axios.post<ArticleRes>('/api/article/searchOwner', page)
    return result.data
}

//上传文件
export async function upload(form:FormData) {
    let result = await axios.post<BaseRes>('/api/file/upload',form,{
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    })
    return result.data
}

//添加
export async function addArticle(article:IArticle) {
    let result = await axios.post<BaseRes>('/api/article/add', article)
    return result.data
}

//获取
export async function getArticle(id:string) {
    let result = await axios.get<ArtGetRes>(`/api/article/${id}`)
    return result.data
}

//更新
export async function putArticle(id:string,article:IArticle) {
    let result = await axios.put<BaseRes>(`/api/article/${id}`,article)
    return result.data
}

//删除
export async function delArticle(id:string) {
    let result = await axios.delete<BaseRes>(`/api/article/${id}`)
    return result.data
}